package org.itboys.ace.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.admin.entity.AdminOrg;
import org.itboys.admin.service.AdminOrgService;
import org.itboys.admin.tools.AdminPermissionCheck;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.AceJsonPageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/***
 * 用户角色管理
 * 
 * @author cxj123
 *
 */
@RestController
@RequestMapping("/ace/admin/org")
public class AceAdminOrgController extends BaseController {
	@Autowired
	private AdminOrgService adminOrgService;

	/**
	 * 点击部门管理跳转页面
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping("/org-list")
	public ModelAndView listvm(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		return new ModelAndView("/ace/admin/org-list");
	}

	/***
	 * 查询所有列表数据
	 * 
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping("/list")
	@AdminPermissionCheck("allData")
	public void list(HttpServletRequest request, HttpServletResponse response,
			Model model) {
		List<AdminOrg> orgList = adminOrgService.getAllOrgsSimple();
		AceJsonPageUtils.renderJsonPage(
				orgList != null && orgList.size() > 0 ? orgList.size() : 0L,
				orgList, response);
	}

	/***
	 * 保存部门菜单列表
	 * 
	 * @param adminOrg
	 * @param response
	 */
	@ResponseBody
	@RequestMapping("/saveOrg")
	public void save(@ModelAttribute AdminOrg adminOrg,
			HttpServletResponse response) {
		try {
			if (adminOrg.getId() == 0l) {
				adminOrgService.doSave(adminOrg);
			} else {
				adminOrgService.doUpdate(adminOrg);
			}
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("adminOrg save error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}

	/***
	 * 根据id查询指定部门信息
	 * 
	 * @param id
	 * @param response
	 */
	@RequestMapping("/getOrg/{id}")
	public void getOrg(@PathVariable("id") Long id, HttpServletResponse response) {
		AjaxUtils.renderJson(response, adminOrgService.getById(id));
	}

	/**
	 * 根据id删除列表
	 * 
	 * @param id
	 * @param response
	 */
	@RequestMapping("/delete/{id}")
	public void delete(@PathVariable("id") Long id, HttpServletResponse response) {
		try {
			AdminOrg org = adminOrgService.getById(id);
			if (org != null) {
				if (org.getParentId() == 0l) {
					// 如果删除的是父亲节点，那么相应的子节点也将会被删除
					List<AdminOrg> childOrg = adminOrgService.findChilidsByParentid(id);
					if (childOrg.size() > 0) {
						for (AdminOrg adminOrg : childOrg) {
							adminOrgService.deleteOrg(adminOrg.getId());
						}
					}
					adminOrgService.deleteOrg(id);
				}else {
					// 如果删除的是子节点，直接进行删除操作就ok
					adminOrgService.deleteOrg(id);
				}
			} 
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("adminOrg delete error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
}
